import Toast from 'react-simple-toast';


/*
get Object to map
 */
export const getObject = (num) => {
	let obj = [];
	for (let i = 0; i < num; i++) {
		obj.push({})
	}
	return obj;
}


/*
get date
 */
export const getDay = (day = 0) => {
	let today = new Date();
	let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
	today.setTime(targetday_milliseconds); //注意，这行是关键代码    
	let tYear = today.getFullYear();
	let tMonth = today.getMonth() + 1;
	let tDate = today.getDate();
	tMonth = tMonth < 10 ? '0' + tMonth : tMonth;
	tDate = tDate < 10 ? '0' + tDate : tDate;
	return tYear + "-" + tMonth + "-" + tDate;
}

/*
check data
 */
export const checkResult = (data) => {
	return data.result === 'RC100';
}

export const getDayName = (day) => {
	var day = parseInt(day);
	if (isNaN(day) || day < 1 || day > 7)
		return false;
	var weekday = [ "星期六","星期天","星期一", "星期二", "星期三", "星期四", "星期五"];
	return weekday[day];
}

/*
get next five day (workday)
 */
export const getFiveDay = () => {
	let dayList = [];
	var d = new Date();
	var arr = [];
	for (var i = 0; dayList.length < 6 && i < 10; i++) {
		let m = d.getMonth() + 1;
		let day = d.getDate();
		m = m < 10 ? `0${m}` : m;
		day = day < 10 ? `0${day}` : day;
		if (d.getDay() !== 0 && d.getDay() !== 6) {
			dayList.push({
				day: m + '月' + day + '日',
				week: getDayName(d.getDay())
			})
		}
		d.setDate(d.getDate() + 1);
	}
	if (new Date().getHours() > 9) {
		dayList = dayList.slice(1, 6);
	} else {
		dayList = dayList.slice(0, 5);
	}
	return dayList;
}

/*
toast
type : msg | loading | success | fail | offline
 */
export const toast = {
	duration: 3000,
	text(msg) {
		Toast({
			type: "msg",
			msg: msg.toString(),
			duration: this.duration,
		})
	},
	success(msg) {
		Toast({
			type: "success",
			msg: msg.toString(),
			duration: this.duration,
		})
	},
	error(msg) {
		if (msg !== undefined) {
			Toast({
				type: "fail",
				msg: msg.toString(),
				duration: this.duration,
			})
		}
	},
	offline(msg) {
		Toast({
			type: "offline",
			msg: msg.toString(),
			duration: this.duration,
		})
	},
	loading(msg) {
		Toast({
			type: "loading",
			msg: msg.toString(),
			typeStatus: 1
		})
	},
	loaded(msg) {
		Toast({
			type: "loading",
			msg: msg.toString(),
			typeStatus: 2
		})
	},

}